package com.example.porject3;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;

public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.PhotoHolder> {

    private Context mContext;
    private ArrayList<String> list;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    private int ifLoad;

    public PhotoAdapter(Context context) {
        this.list = new ArrayList<>();

        // 原本给的都是些小图,增加一些壁纸大图
        this.list.add("https://hbimg.huabanimg.com/d8784bbeac692c01b36c0d4ff0e072027bb3209b106138-hwjOwX_fw658");
        this.list.add("http://pic1.win4000.com/wallpaper/c/584e0d493805a.jpg");
        this.list.add("https://hbimg.huabanimg.com/6215ba6f9b4d53d567795be94a90289c0151ce73400a7-V2tZw8_fw658");
        this.list.add("http://pic1.win4000.com/wallpaper/c/584e0d4b7c532.jpg");
        this.list.add("https://hbimg.huabanimg.com/834ccefee93d52a3a2694535d6aadc4bfba110cb55657-mDbhv8_fw658");
        this.list.add("http://pic1.win4000.com/wallpaper/c/584e0d4f485ae.jpg");
        this.list.add("https://hbimg.huabanimg.com/f3085171af2a2993a446fe9c2339f6b2b89bc45f4e79d-LacPMl_fw658");
        this.list.add("http://pic1.win4000.com/wallpaper/3/57eb318b846a2.jpg");
        this.list.add("https://hbimg.huabanimg.com/e5c11e316e90656dd3164cb97de6f1840bdcc2671bdc4-vwCOou_fw658");
        this.list.add("http://pic1.win4000.com/wallpaper/c/584e0d4725881.jpg");

        this.mContext = context;
        // 创建DisplayImageOptions对象并进行相关选项配置
        this.options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)// 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.ic_launcher)// 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.ic_launcher)// 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
                .displayer(new RoundedBitmapDisplayer(20))// 设置成圆角图片
                .build();// 创建DisplayImageOptions对象
        // 使用ImageLoader加载图片
        this.imageLoader = ImageLoader.getInstance();

        this.ifLoad = 1;
    }

    @NonNull
    @Override
    public PhotoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 以recycler_item作为recyclerView的基本单元
        return new PhotoHolder(LayoutInflater.from(mContext).inflate(R.layout.recycler_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull PhotoHolder holder, int position) {
        // 使用ImageLoader加载图片
        imageLoader.displayImage(this.list.get(position),
                holder.imageView, this.options);
    }

    @Override
    public int getItemCount() {
        if (this.ifLoad == 1)
        {
            return list.size();
        }
        else return 0;
    }

    class PhotoHolder extends  RecyclerView.ViewHolder {

        private ImageView imageView;

        public PhotoHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image_item);
        }
    }

    public void load() {
        if (this.ifLoad == 1)
            return;
        this.ifLoad = 1;
        notifyDataSetChanged();
    }

    public void clear() {
        if (this.ifLoad == 0)
            return;
        this.ifLoad = 0;
        notifyDataSetChanged();
    }

    public void delete() {
        this.imageLoader.clearMemoryCache();
        this.imageLoader.clearDiskCache();
    }
}
